<template>
  <div>
    <TopFilter ref="queryMove" :list="topFilterList" :cols="3" :collapse="true" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="getCustomerDetail"
      :params="fetchParams"
      :datakey="'rows'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="columns => (this.selectedRows = columns)"
      columnsRef="partPartOutboundStockremovalQueryMoveNo"
      selectionType="single"
    ></FilterTable>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <div>
        <el-button @click="closeDrawer()">取 消</el-button>
        <el-button type="primary" @click="saveDrawer()">确 定</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { customerDetail } from '@/api/part/partOutbound/stockremoval/index';
import { getUser } from '@/assets/js/auth';
import Cookies from 'js-cookie';
import { searchRoleByCode, searchEmployee, findByEmployeeId } from '@/api/common/empSelect/index';
import { getDealerUser } from '@/api/customerRepair/customerComplaint/customerComplaintReception';
import { arrayObjDeWeight } from '@/utils/index.js';
import moment from 'moment';

export default {
  name: 'queryMoveNo',
  mixins: [dictionary],
  props: ['moveNo', 'salerList', 'ptype'],
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createTopFilterList(),
      columns: this.createTablelist(),
      selectedRows: [],
      getCustomerDetail: customerDetail,
      fetchParams: { xhrAbort: true }
    };
  },
  created() {
    this.fetchParams = {
      xhrAbort: false,
      transferNo: this.moveNo || '',
      ptype: this.ptype
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
    // this.initData();
    this.getDealerUser();
  },
  methods: {
    createTopFilterList() {
      return [
        {
          label: '移库单号',
          fieldName: 'transferNo',
          type: 'INPUT'
        },
        {
          type: 'RANGE_DATE',
          label: '移库日期',
          fieldName: 'start|end',
          placeholder: '移库日期',
          dateType: 'daterange'
        },
        {
          label: '是否出库',
          fieldName: 'isFinished',
          type: 'SELECT',
          itemList: this.createDictList(1004)
        }
      ];
    },
    createTablelist() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '移库单号',
          dataIndex: 'transferNo',
          sorter: true,
          showOverflowTooltip: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '是否入库',
          dataIndex: 'isFinished',
          sorter: true,
          showOverflowTooltip: true,
          dictItems: this.createDictList('1004'),
          filterItems: this.createDictList('1004')
        },
        {
          title: '移库日期',
          dataIndex: 'transferDate',
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          showOverflowTooltip: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '经手人',
          dataIndex: 'handler',
          sorter: true,
          showOverflowTooltip: true,
          filterItems: [],
          render: params => {
            return <span>{this.salerList[params.row.handler]}</span>;
          }
        },
        {
          title: '锁定人',
          dataIndex: 'lockUser',
          sorter: true,
          showOverflowTooltip: true,
          filterItems: [],
          render: params => {
            return <span>{this.salerList[params.row.lockUser]}</span>;
          }
        }
      ];
    },
    // async getCustomerDetail() {
    //   let res = await customerDetail(this.moveNo == '' ? '' : this.moveNo);
    //   console.log(res.data.rows[0].handler);
    //   // let res1 = findByEmployeeId({ employeeNo: res.data.rows[0].lockUser });
    //   // console.log(111111, res1);
    //   return res;
    // },
    // 对后台取出来的值进行转换
    ischange(sql) {
      if (sql == 10041001) {
        return 1;
      } else {
        return 0;
      }
    },
    // initData() {
    //   let arr = [];
    //   for (let [key, value] of Object.entries(this.salerList)) {
    //     arr.push({ text: value, value: key });
    //   }
    //   this.columns.find(x => x.dataIndex == 'handler').filterItems = this.columns.find(x => x.dataIndex == 'lockUser').filterItems = arr;
    // },
    async getDealerUser() {
      const res = await getDealerUser();
      let data = arrayObjDeWeight(res.data, 'employeeName');
      this.columns.find(x => x.dataIndex == 'handler').filterItems = this.columns.find(x => x.dataIndex == 'lockUser').filterItems = data.map(x => ({
        text: x.employeeName,
        value: x.userId + ''
      }));
    },
    changeHandle(val) {
      if (val.start) {
        val.start = moment(val.start).format('YYYY-MM-DD');
      }
      if (val.end) {
        val.end = moment(val.end).format('YYYY-MM-DD');
      }
      let data = {
        start: val.start,
        end: val.end,
        transferNo: val.transferNo,
        isFinished: val.isFinished
      };
      this.fetchParams = Object.assign({}, data);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    closeDrawer() {
      this.$emit('close', false);
    },
    saveDrawer() {
      if (this.selectedRows.length <= 0) {
        this.$notify.error({ title: '提示信息', message: '请先选择数据!' });
      } else {
        let lock_user = this.selectedRows[0].lockUser;
        if (lock_user) {
          if (lock_user !== Cookies.get('userId')) {
            this.$notify.error({ title: '提示信息', message: '被锁定不可操作!' });
          } else {
            this.$emit('close', this.selectedRows);
          }
        } else {
          this.$emit('close', this.selectedRows);
        }
      }
    }
  }
};
</script>
